package com.sina.shop.manage.controller.foot;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.foot.Foot;
import com.sina.shop.manage.service.foot.FootManageService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-15-13:38
 */
@Controller
@RequestMapping("footManage")
public class FootManageController extends BaseController {

    @Autowired
    @Qualifier("footManageService")
    private FootManageService footManageService;

    @RequestMapping(value = "getFootList", method = RequestMethod.GET)
    public ModelAndView getFootList(@Valid Foot foot) {
        LOG.info("foot:{}", foot);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = footManageService.getFootList(foot, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "deleteFoot", method = RequestMethod.POST)
    public ModelAndView deleteFoot(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            footManageService.deleteFoot(id);
            result.put(CODE, OK);
            result.put(MSG, "删除足迹成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "updateFoot", method = RequestMethod.POST)
    public ModelAndView updateFoot(@Valid Foot foot) {

        LOG.info("foot:{}", foot);


        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            footManageService.updateFoot(foot);
            result.put(CODE, OK);
            result.put(MSG, "更新足迹成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "saveFoot", method = RequestMethod.POST)
    public ModelAndView saveFoot(@Valid Foot foot) {

        LOG.info("foot:{}", foot);

        /*创建时间、修改时间、创建人*/
        foot.setCreateTime(LocalDateTime.now());


        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            footManageService.saveFoot(foot);
            result.put(CODE, OK);
            result.put(MSG, "新增足迹成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getFootListToHtml", method = RequestMethod.GET)
    private ModelAndView getFootListToHtml() {

        ModelAndView mv = new ModelAndView("foot/footList");

        return mv;
    }
}
